var express = require('express');
var router = express.Router();
const studentService = require('../service/studentService');

router.get('/',async function(req,res){
    let {type,value,current,pageSize} = req.query;
    let searchObj = {};
    if(type){
        if(type == "age"){
            searchObj[type] = value;
        }else{
            searchObj[type] = {$regex:value};
        }
    }
    let data = await studentService.find(searchObj,
            {current:current || 1,pageSize:pageSize || 5});
        
    res.send({data});
});

router.get('/:id',async function(req,res){
    let {id} = req.params;
    let data = await studentService.findById(id);
    res.send({data});
});
router.post('/',async function(req,res){
    let {name,age,gender,cls,headImg} = req.body;
    let data = await studentService.insert({name,age,gender,cls,headImg});
    res.send({data});
});
router.delete('/:id',async function(req,res){
    let {id} = req.params;
    let data = await studentService.remove(id);
    res.send({data});
});
router.put('/:id',async function(req,res){
    let {id} = req.params;
    let {name,age,gender,cls} = req.body;
    let data = await studentService.update(id,{name,age,gender,cls});
    res.send({data});
});
module.exports = router;